package servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.CommentDao;
import dao.GoodsDao;
import dao.UserDao;
import entity.Comment;
import entity.Goods;
import entity.User;




@WebServlet("/details")
public class DetailsServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");//这是一个坑，前端传数据进来，要先判断它编码是否正确
		resp.setContentType("text/html;charset=UTF-8");//防止中文乱码
        // 获取Session中的用户uid
        HttpSession session = req.getSession();
        String ud = (String) session.getAttribute("uid");
		String gid = req.getParameter("gid");
		req.setAttribute("gid", gid);
		int gd= Integer.parseInt(gid);
		
//		int gid = Integer.parseInt(req.getParameter("gid"));
//		int uid = Integer.parseInt(req.getParameter("uid"));
		
		//商品收藏数量
//		req.setAttribute("wish_num", "3");
//		req.setAttribute("wish_num", "3");//收藏数量
//		req.setAttribute("cart_num", "3");//购物车商品数量
		
		GoodsDao g = new GoodsDao();
		CommentDao c = new CommentDao();
		UserDao u = new UserDao();
		Goods shop = new Goods();
		User uuu= new User();
		shop.setGid(gd);
		
		try {
			shop = g.queryById(shop);
			List<Comment> clist = c.queryByGId(shop.getGid());			
			List<User> ulist = new ArrayList<User>();			
			int uu;
			req.setAttribute("clist", clist);			
			for(Comment com: clist) {
				uu = com.getUid();
				
				uuu =u.queryById(uu);
				ulist.add(uuu);
			}
			req.setAttribute("ulist", ulist);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		req.setAttribute("title", shop.getGname());	
		req.setAttribute("price", shop.getPrice());		
		req.setAttribute("oldprice", shop.getOldprice());		
		req.setAttribute("img", shop.getPhoto());
		req.setAttribute("grade", shop.getGrade());	//评分
		req.setAttribute("sale", shop.getSale());	//折扣
		req.setAttribute("info", shop.getInfo());	//商品详情
		req.setAttribute("amount", shop.getAmount());	//库存数量
		
		req.getRequestDispatcher("details.jsp").forward(req, resp);

	}
}